package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.mq.config.MqConst;
import com.atguigu.gmall.payment.mapper.OrderInfoMapper;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentService;
import com.atguigu.gmall.service.RabbitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.function.Predicate;

/**
 * @author ：MJS
 * @date ：Created in 2021/5/27  11:29
 * @description：TODO
 * 支付表实现类
 */
@Service
public class PaymentServiceImpl implements PaymentService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private RabbitService rabbitService;
    //保存支付表
    //可能存在幂等性问题
    @Override
    public PaymentInfo insert(Long orderId, PaymentType paymentType) {
        //1.查询
        QueryWrapper<PaymentInfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(queryWrapper);
        if(null==paymentInfo){
            //2.保存
            paymentInfo=new PaymentInfo();
            OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
             //对外业务编号   ATGUIGU+时间戳+1000内的随机数
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            //订单id
            paymentInfo.setOrderId(orderId);
            //支付类型
            paymentInfo.setPaymentType(paymentType.name());
            //总金额
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            //交易内容
            paymentInfo.setSubject(orderInfo.getTradeBody());
            //支付状态
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            //创建时间
            paymentInfo.setCreateTime(new Date());
            paymentInfoMapper.insert(paymentInfo);
        }else{
         //修改支付方式
        paymentInfo.setPaymentType(paymentType.name());
        paymentInfoMapper.updateById(paymentInfo);
        }
        //返回支付表的对象
        return paymentInfo;
    }
    //更新支付表
    @Override
    public void updateStatus(Map<String, String> paramsMap) {
        //防止幂等性问题
        QueryWrapper<PaymentInfo> queryWrapper=new QueryWrapper();
        queryWrapper.eq("out_trade_no",paramsMap.get("out_trade_no"));
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(queryWrapper);
        if(null !=paymentInfo && PaymentStatus.UNPAID.name().equals(paymentInfo.getPaymentStatus())){
            //更新已支付
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //银行流水号
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            //付款时间
            paymentInfo.setCallbackTime(new Date());
            //异步回调的内容设置
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            //更新
            paymentInfoMapper.updateById(paymentInfo);

            System.out.println("交付表更新成功");

            //发送消息MQ  更新订单状态  发送  订单微服务
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

        }
    }

    @Override
    public PaymentInfo select(String outTradeNo) {
        QueryWrapper<PaymentInfo>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("out_trade_no",outTradeNo);
        return paymentInfoMapper.selectOne(queryWrapper);
    }
}
